<?php
namespace My\Controller;

use Utuo\BackgroundController;

class DistributionShopScoreEventController extends BackgroundController {
    
	public function _initialize() {
		$this->setControllerTitle("积币管理");
		$this->setActionTitle(array(
		    'currencyConfig' => '积币基础配置',
			'index' => '积币项列表',
			'modify' => '配置',
		));
		$this->setActionNav("currencyConfig,index");
	}
	
	/**
	 * 商城流通币配置
	 */
	public function currencyConfigAction() {
	    $distributionShop = D("DistributionShop")->where("cid=%d", $this->aid)->find();
	    if ( $distributionShop and empty($distributionShop['currency_config']) ) {
	        $distributionShop['currency_config'] = array(
	            'opened' => 0,
	            'name' => '',
	            'value' => '0',
	        );
	    } else {
	        $distributionShop['currency_config'] = unserialize($distributionShop['currency_config']);
	    }
	    $this->assign('currencyConfig', $distributionShop['currency_config']);
	    $this->display();
	}
	
	/**
	 * 商城流通币配置
	 */
	public function saveCurrencyConfigAction() {
	    $config = I("currencyConfig", array());
	    $config['opened'] = $config['opened']?1:0;
	    if ( false !== D("DistributionShop")->where("cid=%d", $this->aid)->save(array(
	      'currency_config' => serialize($config),
	    )) ) {
	        $this->ajaxSuccess("保存成功");
	    }
	    $this->ajaxError("保存失败");
	}
	
	/**
	 * 积分项列表
	 * @see \Utuo\BackgroundController::indexAction()
	 */
	public function indexAction() {
	    $d = D("DistributionShopScoreEvent");
	    $configList = $d->where(array(
            	        'tag' => array('in', array_keys($d->scoreMethod)),
            	        'cid' => $this->aid,
            	    ))->getField("tag,status");
	    $scoreMethod = $d->scoreMethod;
	    foreach ( $configList as $k => $v ) {
	        $scoreMethod[$k]['status'] = $v;
	    }
	    $this->assign('scoreMethodList', $scoreMethod);
	    $this->display();
	}
	
	/**
	 * 积分项配置
	 * @see \Utuo\BackgroundController::modifyAction()
	 */
	public function modifyAction() {
	    $eventTag = I('tag', '', 'htmlspecialchars_decode');
	    $d = D("DistributionShopScoreEvent");
	    $config = $d->where("cid=%d and tag='%s'", $this->aid, $eventTag)->find();
	    $scoreConfig = $d->scoreMethod[$eventTag];
	    if ( $config ) {
	        $cfg = $config['cfg'];
	        $cfg = (array)unserialize($cfg);
	        foreach ($scoreConfig['fields'] as $k => &$v) {
	            $v['value'] = $cfg[$k];
	        }
	    } else {
	        $data = array(
	            'cid' => $this->aid,
	            'tag' => $eventTag,
	            'status' => 0,
	            'is_deleted' => 0,
	        );
	        $data['id'] = $d->add($data);
	        $config = $data;
	    }
	    $config["cfg"] = $scoreConfig;
	    $config["status"] = $this->getRadio("status", $d->status, $config['status']);
	    $this->assign("config", $config);
	    $this->display();
	}
	
	// 提交修改菜单
	public function submitPost($model) {
	    $model = is_object($model) ? $model : D($model);
	    $eventTag = I('tag', '', 'htmlspecialchars_decode');
	    if ( empty($_REQUEST['id']) ) {
	        $config = $model->where("cid=%d and tag='%s'", $this->aid, $eventTag)->find();
	        if ( $config ) {
	            $_POST['id'] = $config['id'];
	        }
	    }
	    if ($model->isCreated or $model->create()) {
	        if (empty($_REQUEST['id'])) {
	            unset($model->id);
	            if ($model->add()) {
	                $this->ajaxSuccess("添加成功！");
	            } else {
	                $this->ajaxError("添加失败！" . $model->getError() . $model->getLastSql());
	            }
	        } else {
	            if (false !== $model->save()) {
	                $this->ajaxSuccess("修改成功！");
	            } else {
	                $this->ajaxError("修改失败！");
	            }
	        }
	    } else {
	        $this->ajaxError($model->getError());
	    }
	}
	
    
}